package com.ih.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ih.pojo.Customer;
import com.ih.pojo.History;
import com.ih.pojo.Worker;
import com.ih.service.CustomerService;
import com.ih.service.HistoryService;
import com.ih.service.WorkerService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/history")
public class HistoryController {
    @Autowired
    private HistoryService historyService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private WorkerService workerService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/listHis")
    public String listHistory(Model model, @RequestParam Map<String,Object> map,@RequestParam(required = false,defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(required = false,defaultValue = "10",value = "pageSize")Integer pageSize ){

        if (pageNum == null || pageNum <= 0 || pageNum.equals("")){
            pageNum=1;
        }
        if (pageSize==null||pageSize<=0 || pageSize.equals("")){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<History> pageInfo = historyService.listHistory(map, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "historyList";
    }

    @RequestMapping("/preSaveHis")
    public String preSaveHis(Model model){
        List<Customer> custList = customerService.listCustomer();
        List<Worker> workerList = workerService.queryWorker();
        model.addAttribute("custList",custList);
        model.addAttribute("workerList",workerList);
        return "historySave";
    }

    @RequestMapping("saveHis")
    public String saveHis(Integer workerId,Integer custId,History history){
        Worker worker = new Worker();
        worker.setWid(workerId);

        Customer customer = new Customer();
        customer.setCid(custId);

        history.setCustomer(customer);
        history.setWorker(worker);
        int i = historyService.saveHistory(history);
        //清空redis
        redisTemplate.delete("mainMenu");
        //再次查询数据库
        return "redirect:/history/listHis";

    }

    @RequestMapping("/preUpdateHis/{hid}")
    public String preUpdateHis(@PathVariable("hid")Integer hid,Model model){
        List<Customer> custList = customerService.listCustomer();
        List<Worker> workerList = workerService.queryWorker();
        History history = historyService.getHistoryById(hid);
        model.addAttribute("custList",custList);
        model.addAttribute("workerList",workerList);
        model.addAttribute("history",history);
        System.out.println(history);
        return "historyUpdate";
    }

    @PostMapping("/updateHis")
    public String updateHis(Integer workerId,Integer custId,History history){
        Worker worker = new Worker();
        worker.setWid(workerId);
        Customer customer = new Customer();
        customer.setCid(custId);
        history.setCustomer(customer);
        history.setWorker(worker);
        historyService.updateHistory(history);
        redisTemplate.delete("mainMenu");
        return "redirect:/history/listHis";
    }

    @PostMapping("/batchDeleteHistory")
    @ResponseBody
    public String batchDeleteHistory(String idList){
        System.out.println("idList = " + idList);
        int row = historyService.batchDeleteHistory(idList);
        if (row>0){
            return "ok";
        }else {
            return "error";
        }
    }
}
